package deckbuilder;

import javax.swing.*;

import java.util.Random;

public class Flashcard 
{
	public int current = 0;
	public Deck deck = new Deck();
	public JLabel term = new JLabel();
	public JLabel def = new JLabel();
	public JLabel result = new JLabel();
	public JButton op1 = new JButton();
	public JButton op2 = new JButton();
	public JButton op3 = new JButton();
	public JButton next = new JButton("NEXT");
	
	public Flashcard()
	{
		
	}
	
	//http://www.javapractices.com/topic/TopicAction.do?Id=62
	private static int showRandomInteger(int aStart, int aEnd, Random aRandom){
	    if ( aStart > aEnd ) {
	      throw new IllegalArgumentException("Start cannot exceed End.");
	    }
	    //get the range, casting to long to avoid overflow problems
	    long range = (long)aEnd - (long)aStart + 1;
	    // compute a fraction of the range, 0 <= frac < range
	    long fraction = (long)(range * aRandom.nextDouble());
	    int randomNumber =  (int)(fraction + aStart);  
	    return randomNumber;
	  }
	
	public void newLabels()
	{
		term.setText(deck.getCard(current).getTerm());
		def.setText(deck.getCard(current).getDefinition());
	}
	
	public void nextCard()
	{
		if(current >= deck.size())
		{
			current = 0;
		}
		else
		{
			current++;
		}
		
		term.setVisible(true);
		def.setVisible(false);
	}
	
	public void swapLabels()
	{
		if (term.isVisible()) 
		{
			def.setVisible(true);
			term.setVisible(false);
		} 
		else 
		{
			term.setVisible(true);
			def.setVisible(false);
		}
	}
	
	public void newButtons()
	{
		int START = 0;
	    int END = deck.size();
	    Random random = new Random();
	    
	    int fake1 = showRandomInteger(START, END, random);
	    int fake2 = showRandomInteger(START, END, random);
	    
	    while(fake1 == fake2 || fake1 == current || fake2 == current)
	    {
	    	fake1 = showRandomInteger(START, END, random);
	    	fake2 = showRandomInteger(START, END, random);
	    }
	    
	    op1.setText(deck.getCard(current).getDefinition());
	    op2.setText(deck.getCard(fake1).getDefinition());
	    op3.setText(deck.getCard(fake2).getDefinition());	    
	}
}

